package com.zhou;

import com.zhou.spider.Main;

import java.io.IOException;
import java.util.*;

/**
 * @Author: zhouweixin
 * @Description:
 * @Date: Created in 15:26 2019/1/1
 * @Modified By:
 */
public class DownloadZip {
    public static void main(String[] args) {
        Main main = new Main();

        while (true) {
            System.out.println("");
            System.out.println("=============================");
            System.out.println("请输入数字选择要进行的操作(1,2,4需要网络)：");
            System.out.println("1.爬取压缩文件列表");
            System.out.println("2.下载压缩文件");
            System.out.println("3.提取专利号");
            System.out.println("4.下载数据页");
            System.out.println("5.提取专利数据");
            System.out.println("0.退出");
            System.out.println("==============================");
            System.out.println();

            Scanner scanner = new Scanner(System.in);
            int num = scanner.nextInt();
            switch (num) {
                case 0:
                    return;
                case 1:
                    try {
                        main.testDownloadFileName();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 2:
                    Map<Integer, Integer> year2num = main.getYear2num();

                    if (year2num.size() == 0) {
                        System.out.println("zip文件列表为空，请先执行1");
                        continue;
                    }

                    ArrayList<Integer> years = new ArrayList<>(year2num.keySet());
                    years.sort((y1, y2) -> (y1 - y2));

                    System.out.println("年份\t个数");
                    for (int i = 0; i < years.size(); i++) {
                        int year = years.get(i);
                        int n = year2num.get(year);
                        System.out.println(String.format("%d\t%d", year, n));
                    }

                    System.out.println("请输入要下载的年份(多个年份用英文逗号隔开, 如2002,2003): ");
                    String line = scanner.next();
                    String[] ys = line.replace(" ", "").split(",");
                    for (String y : ys) {
                        int year = Integer.parseInt(y);
                        if (year2num.containsKey(year) == false || year2num.get(year) == 0) {
                            System.out.println("输入年份无数据!");
                        } else {
                            main.downloadByYear(year);
                        }
                    }
                    break;
                case 3:
                    main.testExtractPatentNumbers();
                    break;
                case 4:
                    main.testDownloadHtmlByPatentNum();
                    break;
                case 5:
                    main.testExtractPatentFromHtml();
                    break;
                default:
                    System.out.println("您输入的不合法，请重新输入!");
                    continue;
            }
        }
    }
}
